<!DOCTYPE HTML PUBLIC "-//ORA//DTD CD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>[Chapter 6] 6.8 The break Statement</TITLE>
<META NAME="author" CONTENT="Mark Grand">
<META NAME="date" CONTENT="Thu Jul 31 13:14:36 1997">
<META NAME="form" CONTENT="html">
<META NAME="metadata" CONTENT="dublincore.0.1">
<META NAME="objecttype" CONTENT="book part">
<META NAME="otheragent" CONTENT="gmat dbtohtml">
<META NAME="publisher" CONTENT="O'Reilly &amp; Associates, Inc.">
<META NAME="source" CONTENT="SGML">
<META NAME="subject" CONTENT="java">
<META NAME="title" CONTENT="Java Language Reference">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
</HEAD>
<body vlink="#551a8b" alink="#ff0000" text="#000000" bgcolor="#FFFFFF" link="#0000ee">

<DIV CLASS=htmlnav>
<H1><a href='index.htm'><IMG SRC="gifs/smbanner.gif"
     ALT="Java Language Reference" border=0></a></H1>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch06_07.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><B><FONT FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1">Chapter 6<br>Statements and Control Structures</FONT></B></TD>
<td width=172 align=right valign=top><A HREF="ch06_09.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
</table>

&nbsp;
<hr align=left width=515>
</DIV>
<DIV CLASS=sect1>
<h2 CLASS=sect1><A CLASS="TITLE" NAME="JLR2-CH-6-SECT-8">6.8 The break Statement</A></h2>

<P CLASS=para>
<A NAME="CH06.BREAK"></A>A <tt CLASS=literal>break</tt>
statement transfers control out of an enclosing statement:


<p>
<img align=middle src="./figs/jlr0617.gif" alt="[Graphic: Figure from the text]" width=424 height=46 border=0>

<P CLASS=para>
If a <tt CLASS=literal>break</tt> statement
does not contain an identifier, the statement attempts to transfer
control to the statement that follows the innermost enclosing
<tt CLASS=literal>while</tt>,
<tt CLASS=literal>for</tt>, <tt CLASS=literal>do</tt>, or <tt CLASS=literal>switch</tt>
statement. The Java compiler issues an error message if a
<tt CLASS=literal>break</tt> statement without an identifier occurs without
an enclosing
<tt CLASS=literal>while</tt>, <tt CLASS=literal>for</tt>, <tt CLASS=literal>do</tt>,
or <tt CLASS=literal>switch</tt> statement. Here is an example of a
<tt CLASS=literal>break</tt> statement that contains no identifier:

<DIV CLASS=screen>
<P>
<PRE>
while (true) {
    c = in.read();
    if (Character.isSpace(c)
        break;
    s += (char)c;
}
</PRE>
</DIV>

<P CLASS=para>
In this example,
the <tt CLASS=literal>break</tt> statement is used to exit from the
<tt CLASS=literal>while</tt> loop.

<P CLASS=para>
The innermost <tt CLASS=literal>while</tt>, <tt CLASS=literal>for</tt>, 
<tt CLASS=literal>do</tt>, or <tt CLASS=literal>switch</tt> statement that
encloses the <tt CLASS=literal>break</tt> statement must be in the 
immediately enclosing method or initializer block. In other words,
a <tt CLASS=literal>break</tt> statement cannot be used to leave
a method or initializer block. The <tt CLASS=literal>break</tt> statement 
in the following example is used incorrectly and generates an error:

<DIV CLASS=screen>
<P>
<PRE>
while (true) {
    class X {
        void doIt() {
            break;
        } 
    } 
    new X().doIt();
} 
</PRE>
</DIV>

<P CLASS=para>
If a <tt CLASS=literal>break</tt> statement
contains an identifier, the identifier must be defined as the label
of an enclosing statement. A <tt CLASS=literal>break</tt> statement
that contains an identifier attempts to transfer control to the
statement that immediately follows the statement labeled with that
identifier. Here's an example of a <tt CLASS=literal>break</tt> statement
that contains an identifier:

<DIV CLASS=screen>
<P>
<PRE>
foo:{
    doIt();
    if (n &gt; 4) break foo;
    doIt();
}
</PRE>
</DIV>

<P CLASS=para>
In this example, the <tt CLASS=literal>break</tt> statement transfers
control to the statement following the block labeled <tt CLASS=literal>foo</tt>.

<P CLASS=para>
The label used in a <tt CLASS=literal>break</tt> statement must be in 
the immediately enclosing method or initializer block. 
The <tt CLASS=literal>break</tt> statement 
in the following example is used incorrectly and generates an error:

<DIV CLASS=screen>
<P>
<PRE>
foo: {
    class X {
        void doIt() {
            break foo;
        } 
    } 
    new X().doIt();
} 
</PRE>
</DIV>

<P CLASS=para>
The statement to which a <tt CLASS=literal>break</tt> statement
attempts to transfer control is called the
<I CLASS=emphasis>target statement</I>. If
a <tt CLASS=literal>break</tt> statement occurs inside a <tt CLASS=literal>try</tt>
<A NAME="CH06.FINALLY"></A>
statement, control may not immediately transfer to the target statement.
If a <tt CLASS=literal>try</tt> statement has a <tt CLASS=literal>finally</tt>
clause, the <tt CLASS=literal>finally</tt> block is executed before
control leaves the <tt CLASS=literal>try</tt> statement for any reason.
This means that if a <tt CLASS=literal>break</tt> statement occurs inside
a <tt CLASS=literal>try</tt> statement (but not in its <tt CLASS=literal>finally</tt>
block) and the target statement is outside of the
<tt CLASS=literal>try</tt> statement, the <tt CLASS=literal>finally</tt>
block is executed first, before the control transfer can take place.

<P CLASS=para>
If the <tt CLASS=literal>finally</tt> block contains a <tt CLASS=literal>break</tt>,
<tt CLASS=literal>continue</tt>, <tt CLASS=literal>return</tt>,
or <tt CLASS=literal>throw</tt> statement, the pending control transfer
for the previously executed <tt CLASS=literal>break</tt> statement
is forgotten. Instead, control is transferred to the target of the
<tt CLASS=literal>break</tt>, <tt CLASS=literal>continue</tt>,
<tt CLASS=literal>return</tt>, or <tt CLASS=literal>throw</tt> statement in
the <tt CLASS=literal>finally</tt> block.

<P CLASS=para>
If the <tt CLASS=literal>finally</tt> block does not contain a
<tt CLASS=literal>break</tt>, <tt CLASS=literal>continue</tt>,
<tt CLASS=literal>return</tt>,
or <tt CLASS=literal>throw</tt> statement, the pending control transfer
happens after the <tt CLASS=literal>finally</tt> block is done executing,
unless the target statement is enclosed by another
<tt CLASS=literal>try</tt> statement. If there is another enclosing
<tt CLASS=literal>try</tt>
statement and it has a <tt CLASS=literal>finally</tt> clause,
that <tt CLASS=literal>finally</tt> block is also executed before the
control transfer can take place. Execution proceeds in this manner
until the target statement of the <tt CLASS=literal>break</tt> is executed.

<P CLASS=para>
Here is an example that illustrates a simple scenario:

<DIV CLASS=screen>
<P>
<PRE>
ll:{
    try {
        f = new FileInputStream(fname);
        i = f.read();
        if (i != ' ')
            break ll;
        i = f.read();
    } catch (IOException e) {
        System.out.println("Got an IO Exception!");
        break ll;
    } finally {
        f.close();           // Always executed
    }
    // Only reached if we don't break out of the try
    System.out.println("No breaks");
}
</PRE>
</DIV>

<P CLASS=para>
In this example, a <tt CLASS=literal>break</tt> statement is executed
if one of two things happens. First, if an
<tt CLASS=literal>IOException</tt> is thrown, the <tt CLASS=literal>catch</tt>
clause prints
a message and then executes a <tt CLASS=literal>break</tt> statement.
Otherwise, if the first call to <tt CLASS=literal>read()</tt> does not
return a space, a <tt CLASS=literal>break</tt> statement is executed.
In either case, the <tt CLASS=literal>finally</tt> clause is executed
before control is transferred to the statement following the statement
labeled with <tt CLASS=literal>ll</tt>.

<P CLASS=para>
<b>References</b>
<A HREF="ch02_02.htm#JLR2-CH-2-SECT-2.1">Identifiers</A>;
<A HREF="ch06_02.htm#JLR2-CH-6-SECT-2">Labeled Statements</A>;
<A HREF="ch06_09.htm#JLR2-CH-6-SECT-9">The continue Statement</A>;
<A HREF="ch06_07.htm#JLR2-CH-6-SECT-7.2">The do Statement</A>;
<A HREF="ch06_07.htm#JLR2-CH-6-SECT-7.3">The for Statement</A>;
<A HREF="ch06_10.htm#JLR2-CH-6-SECT-10">The return Statement</A>;
<A HREF="ch06_11.htm#JLR2-CH-6-SECT-11">The throw Statement</A>;
<A HREF="ch06_12.htm#JLR2-CH-6-SECT-12">The try Statement</A>;
<A HREF="ch06_07.htm#JLR2-CH-6-SECT-7.1">The while Statement</A>

</DIV>


<DIV CLASS=htmlnav>

<P>
<HR align=left width=515>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch06_07.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><a href="index.htm"><img src='gifs/txthome.gif' border=0 alt='Home'></a></td>
<td width=172 align=right valign=top><A HREF="ch06_09.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
<tr>
<td width=172 align=left valign=top>Iteration Statements</td>
<td width=171 align=center valign=top><a href="index/idx_0.htm"><img src='gifs/index.gif' alt='Book Index' border=0></a></td>
<td width=172 align=right valign=top>The continue Statement</td>
</tr>
</table>
<hr align=left width=515>

<IMG SRC="gifs/smnavbar.gif" USEMAP="#map" BORDER=0> 
<MAP NAME="map"> 
<AREA SHAPE=RECT COORDS="0,0,108,15" HREF="../javanut/index.htm"
alt="Java in a Nutshell"> 
<AREA SHAPE=RECT COORDS="109,0,200,15" HREF="../langref/index.htm" 
alt="Java Language Reference"> 
<AREA SHAPE=RECT COORDS="203,0,290,15" HREF="../awt/index.htm" 
alt="Java AWT"> 
<AREA SHAPE=RECT COORDS="291,0,419,15" HREF="../fclass/index.htm" 
alt="Java Fundamental Classes"> 
<AREA SHAPE=RECT COORDS="421,0,514,15" HREF="../exp/index.htm" 
alt="Exploring Java"> 
</MAP>
</DIV>

</BODY>
</HTML>
